﻿public class NetHandler_LikeComment : GameServerMessageHandler
{
    public const string Key = "LikeComment";

    public static NetHandler_LikeComment NewHandler()
    {
        return new NetHandler_LikeComment();
    }

    protected override ISerialize NewData() => new LikeCommentRequestData();

    public override void _Execute(TCPClient client, ISerialize _data)
    {
        LikeCommentRequestData requestData = _data as LikeCommentRequestData;

        GameDebug.Log($"{client.userID} like: {requestData.commentUserId}  {requestData?.commentId} likeOrNot:{requestData.likeOrNot}");

        UserServer user = Game.instance.GetUser(client.userID);

        ResponseData response = new ResponseData();

        if (user == null)
        {
            GameDebug.LogError($"找不到用户：{client.userID}");
            response.code = ErrorCode.UserNotFound;
        }
        else
        {
            long requestUser = user.data.id;

            if (requestData != null)
            {
                if (requestData.momentUserId > 0)
                {
                    long opponent = requestData.momentUserId;

                    UserServer opponentUser = Game.instance.GetUser(opponent);
                    if (opponentUser != null)
                    {
                        var moment = opponentUser.data.moments.Find(e => e.momentId == requestData.momentId);
                        if (moment != null)
                        {
                            if(moment.comments != null)
                            {
                                var comment = moment.comments.Find(e => e.commentId == requestData.commentId);
                                if(comment != null)
                                {
                                    if (requestData.likeOrNot)
                                        comment.AddLike(requestUser);
                                    else
                                        comment.RemoveLike(requestUser);

                                    GameDebug.Log($"给{opponent} 发送Like Comment结果事件");

                                    NetEvent_LikeComment netEvent_Like = new NetEvent_LikeComment();
                                    netEvent_Like.likeData = new LikeCommentData();
                                    netEvent_Like.likeData.likes = comment.likes;
                                    netEvent_Like.likeData.momentId = requestData.momentId;
                                    netEvent_Like.likeData.commentId = requestData.commentId;
                                    netEvent_Like.SendToRemote(opponentUser.client);
                                }
                                else
                                {
                                    response.code = ErrorCode.CantFindComment;
                                }
                            }
                            else
                            {
                                response.code = ErrorCode.CantFindComment;
                            }
                        }
                        else
                        {
                            response.code = ErrorCode.CantFindMoment;
                        }
                    }
                  
                    comments table = UserServer.FindInCommentTable(opponent, requestData.commentId);
                    if (table != null)
                    {
                        var likes = users.String2LongList(table.likes);

                        if (requestData.likeOrNot)
                        {
                            if (!likes.Contains(requestUser))
                                likes.Add(requestUser);
                        }
                        else
                        {
                            likes.Remove(requestUser);
                        }
                        likes.Remove(0);
                        int ret1 = comments.UpdateLikes(opponent, requestData.commentId, likes.ToArray());

                        if (ret1 >= 0)
                            response.code = 0;
                        else
                            response.code = -1;
                    }
                    else
                    {
                        response.code = ErrorCode.CantFindComment;
                    }
                }
                else
                {
                    response.code = ErrorCode.InvalidUserId;
                }
            }
            else
            {
                GameDebug.LogError($"requestData == null _data:{_data != null} type:{_data.GetType()}");
                response.code = -1;
            }
        }

        byte[] data = response.Write();
        this.SendResponse(client, Key, this.m_MessageReceived.seq, data);
    }
}


